<?php 
include_once 'UserRegView.php';
include_once 'UserRegHandler.php';
include_once '../Misc/Validation.php';
include_once '../Misc/MessageLogistics.php';
class UserRegController
{
	function DoControll()
	{
		$regXhtml = "";
		$msg = "";

		//Instanser
		$userRegistrationView = new UserRegView();
		$userRegHandler = new UserRegHandler();
		$valid = new Validation();
		$msgLogistics = new MessageLogistics();

		//Kolla om användaren har submittat
		if($userRegistrationView->TriedToSubmit())
		{
			//hämta värden från formuläret
			$formValues = $userRegistrationView->GetFormData();
			var_dump($formValues);
			if($formValues[0] != '' || $formValues[1] != '' || $formValues[2] != '' || $formValues[3] != '' || $formValues[4] != '')
			{
				if($userRegHandler->DoesNameExist($formValues[0]) == false)
				{
					if($userRegHandler->PasswordMatch($formValues[1], $formValues[2]))
					{
						if($userRegHandler->EmailMatch($formValues[3], $formValues[4]))
						{
							if($userRegHandler->PasswordCorrectLength($formValues[1], $formValues[2]))
							{
								//echo "spara";
								$userRegHandler->RegisterUser($formValues[0], $formValues[1], $formValues[3]);
								echo "Du har registrerats!";
							}
							else
							{
								$msg = $msgLogistics->Messages(MessageLogistics::INCORRECT_PASSWOD_LENGTH);
								$xhtml = $userRegistrationView->CreateRegForm($msg);
							}
						}
						else
						{
							$msg = $msgLogistics->Messages(MessageLogistics::EMAIL_MISMATCH);
							$regXhtml = $userRegistrationView->CreateRegForm($msg);
						}
					}
					else
					{
						$msg = $msgLogistics->Messages(MessageLogistics::PASSWORD_MISMATCH);
						$regXhtml = $userRegistrationView->CreateRegForm($msg);
					}
				}
				else
				{
					$msg = $msgLogistics->Messages(MessageLogistics::USERNAME_EXISTS);
					$regXhtml = $userRegistrationView->CreateRegForm($msg);
				}
			}
			else
			{
				$msg = $msgLogistics->Messages(MessageLogistics::MISSING_FORM_VALUES);
				$regXhtml = $userRegistrationView->CreateRegForm($msg);
			}
		}
		else
		{
			$regXhtml = $userRegistrationView->CreateRegForm($msg);
		}		
		return $regXhtml;
	}	
}